package com.msc.mc.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.rmi.server.UID;
import java.util.ArrayList;
import java.util.List;
public class SimManagerImportBean {
	private List<ParameterBean> paramList ;
	private List<ObjectBean> objectBean;
	private List<ObjectListBean> objectListBean; 
	
	
	public List<ParameterBean> getParamList() {
		return paramList;
	}

	public void setParamList(List<ParameterBean> paramList) {
		this.paramList = paramList;
	}

	public List<ObjectBean> getObjectBean() {
		return objectBean;
	}

	public void setObjectBean(List<ObjectBean> objectBean) {
		this.objectBean = objectBean;
	}

	public List<ObjectListBean> getObjectListBean() {
		return objectListBean;
	}

	public void setObjectListBean(List<ObjectListBean> objectListBean) {
		this.objectListBean = objectListBean;
	}
	
	
	public String getUid() {
		UID uID = new UID();
		String string = uID.toString();
		string = string.replace(":", "_");
		string = string.replace("-", "_");
		return string;
	}
	
	/**
	 * 添加属性对象
	 * @param name
	 * @param value
	 * @return
	 */
	public ParameterBean addParam(String name, String value){
		if(paramList== null){
			paramList = new ArrayList<ParameterBean>();
		}
		ParameterBean bean = new ParameterBean();
		bean.setName(name);
		bean.setValue(value);
		paramList.add(bean);
		return bean ; 
	}
	/**
	 * 添加Obejct 对象
	 * @param id
	 * @param oid
	 * @param type
	 * @param createRevision
	 * @param activateRevision
	 * @param artribute
	 * @return
	 */
	public ObjectBean addObject(  String type , String oid   , String createRevision,String activateRevision,List<AttributeBean> artribute){
		if(objectBean == null){
			objectBean = new ArrayList<ObjectBean>();
		}
		ObjectBean object = new ObjectBean();
		object.setId( getUid() );
		object.setType(type);
		
		object.setOid(oid);
		object.setCreateRevision(createRevision);
		object.setActivateRevision(activateRevision);
		object.setAttributes(artribute);
		objectBean.add(object);
		return object;
	}
	
	public ObjectBean addObject( String type , String oid , String createRevision,String activateRevision ){
		return addObject(  type ,oid , createRevision , activateRevision , null );
	}
	
	public ObjectBean addObject( String type  ){
		return addObject(  type , null , null , null );
	}
	
	
 
	/**
	 *  添加List对象
	 * @param type
	 * @param searchExp
	 * @param errorIfNotExists
	 */
	public ObjectListBean addObjectList(  String type,String searchExp , boolean errorIfNotExists ){
		
		if(objectListBean == null){
			 objectListBean = new ArrayList<ObjectListBean>();
		} 
		
		ObjectListBean objectList = new ObjectListBean();  
		objectList.setId( getUid() );
		objectList.setType(type);
		objectList.setSearchExp(searchExp);
		objectList.setErrorIfNotExists(errorIfNotExists);
		objectListBean.add(objectList);
		return objectList;
			
	}
	
	
	public AttributeBean addAttributeBean(String name ,String value , String isKey){ 
		AttributeBean attr = new AttributeBean();
		attr.setIsKey(isKey);
		attr.setName(name);
		attr.setValue(value);
		return attr;
		
	}
	

	public String getXMLString(){ 
		StringBuilder xml =  new StringBuilder( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
		xml.append( "<SimManagerImport xmlns=\"http://www.mscsoftware.com/ns/SimManagerImport\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" );
		if(paramList!= null && paramList.size()>0){
			for (ParameterBean param : paramList) {
				xml.append( param.toString() );
			}
		}
		
		if(objectListBean!= null && objectListBean.size()>0){
			for (ObjectListBean obj : objectListBean) {
				xml.append( obj.toString() );
			}
		}
		
		if(objectBean!= null && objectBean.size()>0){
			for (ObjectBean obj : objectBean) {
				xml.append( obj.toString() );
			}
		}
		
		xml.append( "</SimManagerImport>" );
		return xml.toString();
	}
	
	public File getXMLFile(String filePath , String fileName){
		return text2File(getXMLString(),filePath,fileName);
	}
	
	 /**
	  * 将字符串写入文件中
	  * @param fileContent  文件内容
	  * @param filePath     文件路径
	  * @param fileName     文件名称
	  */
	 public  File text2File(String fileContent,String filePath,String fileName){
		 File file = null;
       try {  
    	   File dir=new File(filePath);  
           if(!dir.exists() && !dir.isDirectory()){//判断文件目录是否存在    
               dir.mkdirs();    
           }  
    	   
           file = new File(filePath+fileName);  
           
           PrintStream ps = new PrintStream(new FileOutputStream(file));  
           ps.println(fileContent);// 往文件里写入字符串  
           ps.close();  
       } catch (FileNotFoundException e) {   
           e.printStackTrace();  
       }  
       return file;
		    
	 }
	
}
	
